<template></template>

<script>
import Stomp from "stompjs";

export default {
  data() {
    return {
      client: null,
    };
  },
  mounted() {
    //初始化连接
    this.connect();
  },
  methods: {
    //webSocket连接成功后回调函数
    connect() {
      this.client = Stomp.client("ws://192.168.66.37:15674/ws");
      this.client.connect(
        "jiazx",
        "jiazx",
        () => {
          console.log("登录成功");
        },
        () => {
          console.log("登录失败");
        },
        "/"
      );
      // var exchange = "/exchange/exchange_pushmsg/rk_pushmsg";
      // //创建随机队列用上面的路由key绑定交换机,放入收到消息后的回调函数和失败的回调函数
      // this.client.subscribe(exchange, this.responseCallback, this.onFailed);
      // console.log(frame);
    },
    responseCallback(frame) {
      console.log("得到的消息 msg=>" + frame.body);
      console.log(frame);
      //接收到服务器推送消息，向服务器定义的接收消息routekey路由rk_recivemsg发送确认消息
      this.client.send(
        "/exchange/exchange_pushmsg/rk_recivemsg",
        { "content-type": "text/plain" },
        frame.body
      );
    },
  },
};
</script>

<style>
</style>